﻿using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace shop.Web.hub
{

    /// <summary> 用户后台运行的查询任务 </summary>
    public class UserJobsHub:Hub
    {

        public static readonly ConcurrentDictionary<string, string> _users = new ConcurrentDictionary<string, string>();


        public override Task OnConnectedAsync()
        {
            var userId = Context.GetHttpContext().Request.Query["userId"];

            _users[userId] = Context.ConnectionId;

            return base.OnConnectedAsync();
        }

        public override Task OnDisconnectedAsync(Exception exception)
        {
            // 
            var user = _users.FirstOrDefault(kv => kv.Value == Context.ConnectionId).Key;
            if (user != null)
            {
                _users.TryRemove(user, out _);
            }


            return base.OnDisconnectedAsync(exception);
        }



    }
}
